import 'dart:async';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:zootix/services/zt_app_info.service.dart';
import 'package:zootix/util/zt_screen_adaper_util.dart';
import 'package:zootix/util/zt_util.dart';
import 'package:zootix/constants/zt_router.dart';

class OnboardingController extends GetxController {
  final PageController pageController = PageController();
  final RxInt currentPage = 0.obs;
  Timer? _timer;
// 页面内容
  List<Widget> pages = [];
  @override
  void onInit() {
    ZtLogManger().putLog(
      ZtLogType.globalBehavior,
      ZtLogManger.buildEvent(
          logType: ZtLogType.globalBehavior, page: ZtLogPages.onboarding),
    );
    pages = List.generate(
        3,
        (index) => Stack(
              alignment: Alignment.center,
              children: [
                Positioned(
                    top: ZtScreenAdaper.height(320),
                    left: 0,
                    right: 0,
                    child: Image(
                        fit: BoxFit.fitHeight,
                        height: ZtScreenAdaper.height(200),
                        image: const AssetImage(
                            'assets/images/onboarding_middle.png'))),
                Positioned(
                  top: ZtScreenAdaper.height(120),
                  child: Image(
                      fit: BoxFit.cover,
                      width: ZtScreenAdaper.screenWidth() -
                          ZtScreenAdaper.width(120),
                      image: AssetImage(
                          'assets/images/onboarding_photo${index + 1}.png')),
                ),
                Positioned(
                    bottom: ZtScreenAdaper.height(150),
                    right: ZtScreenAdaper.width(30),
                    child: SizedBox(
                        child: Text(
                      index == 0
                          ? 'Easy entry to the zoo!'
                          : index == 1
                              ? 'Zoo Online Tickets！'
                              : 'Show order number！',
                      style: TextStyle(
                          fontSize: ZtScreenAdaper.sp(25),
                          fontStyle: FontStyle.italic,
                          fontWeight: FontWeight.bold),
                    ))),
                Positioned(
                    bottom: ZtScreenAdaper.height(100),
                    right: ZtScreenAdaper.width(30),
                    child: InkWell(
                        onTap: () {
                          goToNextPage();
                        },
                        child: Image(
                          fit: BoxFit.cover,
                          width: ZtScreenAdaper.width(30),
                          image: const AssetImage(
                              'assets/images/onboarding_next_btn.png'),
                        ))),
              ],
            ));
    startTimer();
    super.onInit();
  }

  @override
  void onClose() {
    pageController.dispose();
    super.onClose();
  }

  void onPageChanged(int index) {
    currentPage.value = index;
    startTimer();
  }

  void goToNextPage() async {
    if (currentPage.value < pages.length - 1) {
      currentPage.value++;
      pageController.nextPage(
        duration: const Duration(milliseconds: 300),
        curve: Curves.linear,
      );
    } else {
      await ZtAppInfoService.to.setIsFirstInstall();
      _timer?.cancel();
      Get.offAllNamed(ZtRouteConfig.login);
    }
  }

  void startTimer() {
    _timer?.cancel();
    _timer = Timer(const Duration(seconds: 2), () {
      goToNextPage();
    });
  }
}
